package system

import (
	"ruoyi_gin/app/admin/model/tools"
	"ruoyi_gin/utils"
	"ruoyi_gin/utils/R"
	"time"
)

// SysDept model：数据库字段
type SysDept struct {
	DeptId     int       `json:"deptId" gorm:"column:dept_id;primaryKey"` //表示主键
	ParentId   int       `json:"parentId" gorm:"parent_id"`
	Ancestors  string    `json:"ancestors" gorm:"ancestors"`
	DeptName   string    `json:"deptName" gorm:"dept_name"`
	OrderNum   int       `json:"orderNum" gorm:"order_num"`
	Leader     string    `json:"leader" gorm:"leader"`
	Phone      string    `json:"phone" gorm:"phone"`
	Email      string    `json:"email" gorm:"email"`
	Status     string    `json:"status" gorm:"status"`
	DelFlag    string    `json:"delFlag" gorm:"del_flag"`
	CreateBy   string    `json:"createBy" gorm:"create_by"`
	CreateTime time.Time `json:"createTime" gorm:"column:create_time;type:datetime"`
	UpdateBy   string    `json:"updateBy" gorm:"update_by"`
	UpdateTime time.Time `json:"updateTime" gorm:"column:update_time;type:datetime"`
}

type SysDeptResult struct {
	DeptId     int             `json:"deptId" gorm:"column:dept_id;primaryKey"` //表示主键
	ParentId   int             `json:"parentId" gorm:"parent_id"`
	Ancestors  string          `json:"ancestors" gorm:"ancestors"`
	DeptName   string          `json:"deptName" gorm:"dept_name"`
	OrderNum   int             `json:"orderNum" gorm:"order_num"`
	Leader     string          `json:"leader" gorm:"leader"`
	Phone      string          `json:"phone" gorm:"phone"`
	Email      string          `json:"email" gorm:"email"`
	Status     string          `json:"status" gorm:"status"`
	ParentName string          `json:"parentName" gorm:"parent_name"`
	Children   []SysDeptResult `json:"children"`
	CreateBy   string          `json:"createBy" gorm:"create_by"`
	CreateTime time.Time       `json:"createTime" gorm:"column:create_time;type:datetime"`
	UpdateBy   string          `json:"updateBy" gorm:"update_by"`
	UpdateTime time.Time       `json:"updateTime" gorm:"column:update_time;type:datetime"`
}

// TableName 指定数据库表名称
func (SysDept) TableName() string {
	return "sys_dept"
}

func GetDeptList(params tools.SearchTableDataParam, isPaging bool) ([]SysDeptResult, int64) {

	var total int64

	var pageNum = params.PageNum
	var pageSize = params.PageSize

	offset := (pageNum - 1) * pageSize

	dept := params.Other.(SysDept)
	var list []SysDeptResult
	var db = utils.MysqlDb.Model(&[]SysDept{})
	var deptId = dept.DeptId
	if deptId != 0 {
		db.Where("dept_id = ?", deptId)
	}

	var parentId = dept.ParentId
	if parentId != 0 {
		db.Where("parent_id = ?", parentId)
	}

	var deptName = dept.DeptName
	if deptName != "" {
		db.Where("dept_name like concat('%', ?, '%')", deptName)
	}

	var status = dept.Status
	if status != "" {
		db.Where("status = ?", status)
	}
	db.Order("parent_id, order_num")

	if err := db.Count(&total).Error; err != nil {
		return nil, 0
	}
	if isPaging {
		if err := db.Offset(offset).Limit(pageSize).Find(&list).Error; err != nil {
			return nil, 0
		}
	} else {
		if err := db.Find(&list).Error; err != nil {
			return nil, 0
		}
	}
	return list, total

}

func GetDeptInfo(deptId string) SysDept {

	var dept SysDept

	err := utils.MysqlDb.Where("dept_id = ?", deptId).First(&dept).Error

	if err != nil {

		panic(R.ReturnFailMsg(err.Error()))
	}

	return dept

}

func SaveDept(sDept SysDept) R.Result {
	sDept.DelFlag = "0"

	err := utils.MysqlDb.Model(&sDept).Create(sDept).Error

	if err != nil {
		return R.ReturnFailMsg(err.Error())
	}
	return R.ReturnSuccess("操作成功")
}

func UpDateDept(sDept SysDept) R.Result {

	err := utils.MysqlDb.Updates(&sDept).Error

	if err != nil {
		return R.ReturnFailMsg(err.Error())
	}
	return R.ReturnSuccess("操作成功")
}

func DeleteDept(deptId string) R.Result {

	err := utils.MysqlDb.Where("dept_id = ?", deptId).Delete(&SysDept{}).Error
	if err != nil {
		return R.ReturnFailMsg(err.Error())
	}
	return R.ReturnSuccess("操作成功")
}
